{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自编处方界面</title>
    <!-- 引入 layui.css -->
    <link href="{%static '/css/layui.css'%}" rel="stylesheet">
</head>
<body>

<div class="layui-container">
    <div class="layui-form">
        <input type="hidden" id="prescript_id" value="{{prescription_id}}" />
        <input type="hidden" id="drug_id" />
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">订单编号</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="OrderNo" id="OrderNo" value="{{OrderNum}}" lay-verify="required|phone" autocomplete="off" lay-reqtext="订单编号" class="layui-input layui-disabled" disabled/>
                </div>
                <label class="layui-form-label">开药日期</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="date" id="date1" autocomplete="off" lay-reqtext="开药日期" value="{{OrderDt}}" class="layui-input layui-disabled" disabled>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">药品</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="tablet_info" name="tablet_info" lay-verify="required" lay-search>
                        <option value="">请选择药品</option>
                        {% for tablet_obj in tablet_info_list%}
                        <option value="{{tablet_obj.id}}">{{tablet_obj.tblt_nm}}</option>
                        {% endfor %}
                    </select>
                </div>
                <label class="layui-form-label">药品备注</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="MedNote" id="MedNote" autocomplete="off" lay-reqtext="请填写备注" lay-affix="clear" class="layui-input">
                </div>
                <label class="layui-form-label">药品规格</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="MedSpec" id="MedSpec" autocomplete="off" lay-reqtext="请填写药品规格" lay-affix="clear" class="layui-input">
                </div>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">服药备注</label>
                <div class="layui-input-inline layui-input-wrap">
                    <select id="drtscd" name="drtscd" lay-verify="required" lay-search>
                        <option value="">请选择</option>
                        <option value="饭前">饭前</option>
                        <option value="饭后">饭后</option>
                        <option value="饭前或饭后">饭前或饭后</option>
                    </select>
                </div>
                <label class="layui-form-label">服药时间</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="doselist_time" id="doselist_time" autocomplete="off" lay-reqtext="请填写服药时间" class="layui-input">
                </div>
                <label class="layui-form-label">服药剂量</label>
                <div class="layui-input-inline layui-input-wrap">
                    <input type="text" name="doselist_dose" id="doselist_dose" autocomplete="off" lay-reqtext="请填写服药剂量" class="layui-input">
                </div>
            </div>
          </div>
    </div>
    <button type="button" id="savedrug" class="layui-btn">保存</button>
    <table class="layui-table" style="height:full-150" id="infoTable"></table>
</div>

<!--以下是工具栏设置-->
<script id="chufangBar" type="text/html">
    <button type="button" class="layui-btn layui-btn-sm" id="assignbtn" lay-event="assign_event">
        确定{{d.id}}
    </button>
    <button type="button" class="layui-btn layui-btn-sm" id="cancel_btn" lay-event="cancel_event">
        取消
    </button>
</script>
<script id="zhenliaoBar" type="text/html">
    <button type="button" class="layui-btn layui-btn-sm" id="assignbtn" lay-event="edit_event">
        编辑
    </button>
    <button type="button" class="layui-btn layui-btn-sm" id="cancel_btn" lay-event="del_event">
        删除
    </button>
</script>

<!-- 引入 layui.js -->
<script src="{%static '/js/layui.min.js'%}" charset="utf-8"></script>

<script>
    layui.use(['layer','jquery','table','laydate','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            form = layui.form;

        laydate.render({
            elem:"#date1"
        });
        $("#savedrug").click(function(){
           var tablet_options = $("#tablet_info").val()
           if(tablet_options=="")
           {
               layer.msg("请选择药品");
               return;
           }
           drgu_id = $("#drug_id").val()
           if(drgu_id>0){
               $.ajax({
                   url:"/api/tablet/tabletinfo/?id="+tablet_options,
                   type:"GET",
                   dataType:"json",
                   success:function(data){
                       if(data.code="200"){
                           tabletinfo = data.data[0]

                           $.ajax({
                               url:"/api/prescription/drug/"+drgu_id+"/",
                               type:"put",
                               dataType:"json",
                               data:{
                                   "tablet":tablet_options,
                                   "MedCd":tabletinfo.tblt_cd,
                                   "MedUnit":tabletinfo.tblt_unit,
                                   "MedNm":tabletinfo.tblt_nm,
                                   "MedSpec":$("#MedSpec").val(),
                                   "MedNote":$("#MedNote").val(),
                                   "prescrption":{{prescription_id}},
                                   "drtscd":$("#drtscd").val(),
                                   "takedays":1,
                                   "doselist_time":$("#doselist_time").val(),
                                   "doselist_dose":$("#doselist_dose").val()
                               },
                               success:function(data){
                                   if(data.code="200"){
                                       $("#drug_id").val("");
                                       $("#tablet_info").val("");
                                       $("#MedNote").val("");
                                       $("#MedSpec").val("");
                                       $("#drtscd").val("");
                                       $("#doselist_time").val("");
                                       $("#doselist_dose").val("");
                                       form.render('select')
                                       layer.msg(data.msg,{time:1*1000},function(){
                                           table.reload('infoTable',function(){
                                               page:{curr:1}
                                           });
                                       });
                                   }
                               }
                           });
                       }
                   }
               });
           }
           else
           {
               $.ajax({
                   url:"/api/tablet/tabletinfo/?id="+tablet_options,
                   type:"GET",
                   dataType:"json",
                   success:function(data){
                       if(data.code="200"){
                           tabletinfo = data.data[0]

                           $.ajax({
                               url:"/api/prescription/drug/",
                               type:"POST",
                               dataType:"json",
                               data:{
                                   "tablet":tablet_options,
                                   "MedCd":tabletinfo.tblt_cd,
                                   "MedUnit":tabletinfo.tblt_unit,
                                   "MedNm":tabletinfo.tblt_nm,
                                   "MedSpec":$("#MedSpec").val(),
                                   "MedNote":$("#MedNote").val(),
                                   "prescrption":{{prescription_id}},
                                   "drtscd":$("#drtscd").val(),
                                   "takedays":1,
                                   "doselist_time":$("#doselist_time").val(),
                                   "doselist_dose":$("#doselist_dose").val()
                               },
                               success:function(data){
                                   if(data.code="200"){
                                       $("#drug_id").val("");
                                       $("#tablet_info").val("");
                                       $("#MedNote").val("");
                                       $("#MedSpec").val("");
                                       $("#drtscd").val("");
                                       $("#MedSpec").val("");
                                       $("#drtscd").val("");
                                       $("#doselist_time").val("");
                                       $("#doselist_dose").val("");
                                       form.render('select');
                                       table.reload('infoTable',function(){
                                           page:{curr:1}
                                       });
                                   }
                               }
                           });
                       }
                   }
               });
           }
        });

        table.render({
           elem:"#infoTable",
           url:"/api/prescription/drug?prescrption={{prescription_id}}",
           height:'full-215',
           page:true,
           cols:[[
               {toolbar:'#zhenliaoBar',title:"操作",width:140,fixed:'right'},
               {title:'序号',width:70,type:"numbers"},
               {field:'MedCd',title:"药品编号" },
               {field:'MedNm',title:"药品名称" },
               {field:'MedSpec',title:"药品规格" },
               {field:'MedNote',title:"药品备注" },
               {field:'drtscd',title:"服药备注" },
               {field:'doselist_time',title:"服药时间" },
               {field:'doselist_dose',title:"服药剂量" },
           ]],
           //修改参数
           request:{
               "pageName":"pageNum",
               "limitName":"pageSize"
           },
           response:{
               statusName:'code',     //数据状态的字段名称，默认：codes
               countName: 'count',    //数据总数的字段名称，默认：count
               dataName:'data',       //数据列表的字段名称，默认：data
           },
           parseData:function(res){
               d = {
                   "code":0,
                   "count":res.data.count,
                   "data":res.data.results
               }
               return d
           }
        });
        table.on('tool(infoTable)',function(obj){

           if(obj.event == 'edit_event'){
               drug_obj = obj.data;
               $("#drug_id").val(drug_obj.id);
               $("#tablet_info").val(drug_obj.tablet);
               $("#MedNote").val(drug_obj.MedNote);
               $("#drtscd").val(drug_obj.drtscd);
               $("#doselist_time").val(drug_obj.doselist_time);
               $("#doselist_dose").val(drug_obj.doselist_dose);
               form.render('select')
           }
           else if(obj.event == 'del_event'){
               data = obj.data;
               url = "/api/prescription/drug/"+data.id+"/"
               $.ajax({
                   url:url,
                   type:'delete',
                   dataType:"json",
                   success:function(data){
                       if(data){
                           layer.msg(data.msg,{time:1*1000},function(){
                               table.reload('infoTable',function(){
                                   page:{curr:1}
                               });
                           });
                       }
                       else{
                           layer.msg("删除成功",{time:1*1000},function(){
                               table.reload('infoTable',function(){
                                   page:{curr:1}
                               });
                           });
                       }
                   }
               })
           }
       });
    });
</script>
</body>
</html>